<template>
  <el-form ref="form" :model="data" :rules="rules" label-width="100px">
    <el-form-item label="所属项目" prop="project_id">
      <el-select v-model="data.project_id" placeholder="请选择所属项目" clearable @change="select_project" class="filter-item">
        <el-option v-for="(project,index) in project_list" :key="index" :label="project.title" :value="project.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="所属建筑" prop="building_id">
      <el-select v-model="data.building_id" placeholder="请选择所属建筑" clearable class="filter-item">
        <el-option v-for="(building,index) in building_list" :key="index" :label="building.title" :value="building.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="楼层名称" prop="title">
      <el-input v-model.trim="data.title" placeholder="请填写楼层名称" />
    </el-form-item>

    <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
      <img width="100%" :src="previewUrl" alt="">
    </el-dialog>  
    
    <el-form-item>
      <el-button type="primary" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { create } from '@/api/projectfloor'
import { getToken } from '@/utils/auth'
import { commonApi } from '@/api/common' // 公共接口

export default {
  name: 'projectfloorCreate',
  components: { },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    },
    project_list: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      previewUrl: '',
      headerObj: {
        'Authorization': 'Bearer ' + getToken()
      },
      rules: {
        project_id: [ 
          { required: true, message: "请选择所属项目", trigger: "change" }
        ], 
        building_id: [ 
          { required: true, message: "请选择所属建筑", trigger: "change" }
        ],
        title: [ 
          { required: true, message: "请输入楼层名称", trigger: "blur" }
        ], 
      },
      building_list: [],
      data: {
        project_id: '', // 必须添加这个字段
        building_id: '',
        title: '',
      }
    }
  },
  created() {
    // 初始化建筑列表
    if (this.data.project_id) {
      this.select_project(this.data.project_id)
    }
  },
  methods: {
    select_project(value) {
      console.log(value)
      if (!value) {
        this.building_list = []
        this.data.building_id = '' // 清空建筑选择
        return
      }
      
      commonApi.get_building({ project_id: value }).then(response => {
        this.building_list = response.data
      }).catch(error => {
        console.error('获取建筑列表失败:', error)
        this.building_list = []
      })
    },
    
    submit() {
      const thiz = this

      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        
        this.$store.commit('settings/SET_AJAX_BUTTON', true);
        
        create({
          project_id: this.data.project_id,
          building_id: this.data.building_id, // 确保传递了building_id
          title: this.data.title,
        }).then(response => {
          this.$message({
            message: '添加成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
              }
              thiz.item.dialogVisible = false
              thiz.$store.commit('settings/SET_AJAX_BUTTON', false);
            }
          })
        }).catch(error => {
          this.$store.commit('settings/SET_AJAX_BUTTON', false);
          console.error('提交失败:', error)
        })
      })
    }
  }
}
</script>